﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GU.WebERP.DAL;
using YGOP.ESB.Log;
using Newtonsoft.Json.Linq;
using System.Collections;
using GU.WebERP.Core.Utils;
using System.Web.SessionState;
using GU.WebERP.Model;
using GU.WebERP.Core.AjaxFx;

namespace GU.WebERPApp.Ajax.Building
{
    [AjaxFxClass("itm.sys.building", "办公楼API")]
    public class Building
    {
        [AjaxFxMethodAttribute("itm.sys.building.getlist", "获取办公楼城市列表")]
        public AjaxFxRspJson GetList(HttpContext context)
        {
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);

            try
            {
                GU.WebERP.DAL.DAL_Fct_OfficeBuildings dal = new DAL_Fct_OfficeBuildings();

                string cityName = HttpUtility.UrlDecode(context.Request["cityName"]);
                string district = HttpUtility.UrlDecode(context.Request["district"]);
                string buildingName = HttpUtility.UrlDecode(context.Request["buildingName"]);
                int pageSize = context.Request["pageSize"] == null ? 10 : int.Parse(context.Request["pageSize"].ToString());
                int pageIndex = context.Request["pageIndex"] == null ? 0 : int.Parse(context.Request["pageIndex"].ToString());
                string sort = HttpUtility.UrlDecode(context.Request["sort"]);

                var ds = dal.GetList(cityName, district, buildingName, pageSize, pageIndex, sort);
                ajax.MiniUI.Add("total", int.Parse(ds.Tables[0].Rows[0][0].ToString()));
                ajax.MiniUI.Add("data", JToken.FromObject(ds.Tables[1]));
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "获取办公楼列表失败!";

                LogManager.WriteAppError(ex);
            }
            return ajax;
        }

        [AjaxFxMethodAttribute("itm.sys.building.getcitylist", "获取办公楼城市列表")]
        public AjaxFxRspJson GetCityList(HttpContext context)
        {
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);

            try
            {
                DAL_Fct_OfficeBuildings dal = new DAL_Fct_OfficeBuildings();
                var dt = dal.GetCityList();
                ajax.MiniUIJson = JToken.FromObject(dt).ToString();
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "获取办公楼列表失败!";

                LogManager.WriteAppError(ex);
            }
            return ajax;
        }

        [AjaxFxMethodAttribute("itm.sys.building.getdistrictlist", "获取办公楼城市区列表")]
        public AjaxFxRspJson GetDistrictList(HttpContext context)
        {
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);

            try
            {
                GU.WebERP.DAL.DAL_Fct_OfficeBuildings dal = new DAL_Fct_OfficeBuildings();

                var dt = dal.GetDistrictList(HttpUtility.UrlDecode(context.Request["CityName"]));
                ajax.MiniUIJson = JToken.FromObject(dt).ToString();
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "获取办公楼列表失败!";

                LogManager.WriteAppError(ex);
            }
            return ajax;
        }

        [AjaxFxMethodAttribute("gu.building.delete", "删除办公楼")]
        public AjaxFxRspJson DeleteBuilding(HttpContext context)
        {
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);

            try
            {
                GU.WebERP.DAL.DAL_Fct_OfficeBuildings dal = new DAL_Fct_OfficeBuildings();

                var dt = dal.DeleteBuilding(context.Request["BId"]);
                ajax.RspMsg = "删除成功！";
                ajax.RspCode = 1;
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "获取办公楼列表失败!";

                LogManager.WriteAppError(ex);
            }
            return ajax;
        }

        [AjaxFxMethodAttribute("gu.building.get", "获取单个办公楼对象")]
        public AjaxFxRspJson GetBuildingModel(HttpContext context)
        {
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);

            try
            {
                GU.WebERP.DAL.DAL_Fct_OfficeBuildings dal = new DAL_Fct_OfficeBuildings();

                var dt = dal.GetBuildingModel(context.Request["BId"]);

                ajax.MiniUIJson = JToken.FromObject(dt).ToString();
                ajax.RspCode = 1;
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "获取单个办公楼对象!";

                LogManager.WriteAppError(ex);
            }
            return ajax;
        }

        [AjaxFxMethodAttribute("gu.building.exists", "验证办公楼是否存在")]
        public AjaxFxRspJson ExistsBuilding(HttpContext context)
        {
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);

            try
            {
                GU.WebERP.DAL.DAL_Fct_OfficeBuildings dal = new DAL_Fct_OfficeBuildings();

                var city = context.Request["city"];
                var district = context.Request["district"];
                var buildingName = context.Request["buildingName"];

                var exists = dal.ExistsBuilding(city, district, buildingName);

                ajax.MiniUIJson = JToken.FromObject(exists).ToString().ToLower();
                ajax.RspCode = 1;
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "获取单个办公楼对象!";

                LogManager.WriteAppError(ex);
            }
            return ajax;
        }

        [AjaxFxMethodAttribute("gu.building.add_update", "添加、编辑办公楼")]
        public AjaxFxRspJson AddAndUpdateBuilding(HttpContext context)
        {
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);

            try
            {
                Model_Fct_User model=context.Session["User"] as Model_Fct_User;

                GU.WebERP.DAL.DAL_Fct_OfficeBuildings dal = new DAL_Fct_OfficeBuildings();

                var data = context.Request["submitData"];
                var type = context.Request["type"];

                Hashtable hs = new Hashtable();
                hs = Newtonsoft.Json.JsonConvert.DeserializeObject<Hashtable>(data);
                if (string.IsNullOrWhiteSpace(hs["PinYin"].ToString())) //如果未输入拼音
                {
                    var pinyin = StringHelper.GetStringPY(hs["BuildingName"].ToString()).Substring(0, 1);
                    hs["PinYin"] = pinyin;
                }
                else
                {
                    hs["PinYin"] = hs["PinYin"].ToString().ToUpper().Substring(0, 1);
                }

                { //直辖市
                    var bid = hs["ParentId"].ToString();
                    var dt = dal.GetBuildingModel(bid);
                    if (dt == null || dt.Rows.Count == 0)
                    {
                        ajax.RspCode = 0;
                        ajax.RspMsg = "获取AreaID失败";
                        return ajax;
                    }
                    hs.Add("IsZxs", dt.Rows[0]["IsZxs"]);
                    hs.Add("AreaId", dt.Rows[0]["AreaId"]);
                }

                if (type == "EditBuilding")  //编辑
                {
                    hs.Add("Rec_ModifyBy", model.UserName);
                    var edit = dal.UpdateBuilding(hs);
                    
                    ajax.RspCode=edit?1:0;
                    ajax.RspMsg=edit?"更新成功！":"更新失败！";
                    return ajax;
                }
                else if (type == "InsertBuilding")  //添加
                {
                    hs.Add("Rec_CreateBy", model.UserName);
                    var insert = dal.AddBuilding(hs);

                    ajax.RspCode=insert?1:0;
                    ajax.RspMsg=insert?"添加成功！":"添加失败！";
                    return ajax;
                }
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "添加修改办公楼失败!";

                LogManager.WriteAppError(ex);
            }
            return ajax;
        }
    }
}